﻿Imports FM.PsychoFolder.VKeys
Imports System.Runtime.InteropServices
Imports System.Reflection.Assembly

''' <summary>
''' Reads the sendkeys as string and then parses and process
''' </summary>
Public Class KeyProcessor


#Region "PINVOKE"

    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
           CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
           ExactSpelling:=True, SetLastError:=True)> _
Public Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
                              ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    End Sub

#End Region
    '"{TAB}{ENTER}"


    Public Shared Sub Process(ByVal keyString As String)

        Dim Keys() As String = keyString.Split(","c)

        For Each Key As String In Keys
            SendKey(Key)
        Next

    End Sub

    Private Shared Sub SendKey(ByVal Key As String)
        If Key.StartsWith("V") Then
            Dim VKey As Byte = GetKey(Key)
            If VKey = 0 Then Exit Sub

            keybd_event(VKey, 0, 0, 0) 'press
            keybd_event(VKey, 0, 2, 0) 'release

        Else
            'It's not a key, it should sleep time
            Dim Wait As Integer
            If Integer.TryParse(Key, Wait) Then
                Threading.Thread.Sleep(Wait)
            End If

        End If


    End Sub


    'For i As Integer = 0 To 3
    '    keybd_event(VK_TAB, 0, 0, 0)
    '    keybd_event(VK_TAB, 0, 2, 0)
    'Next

    'Thread.Sleep(500)
    'keybd_event(VK_RETURN, 0, 0, 0)
    'keybd_event(VK_RETURN, 0, 2, 0)

    'keybd_event(VK_RETURN, 0, 0, 0)
    'keybd_event(VK_RETURN, 0, 2, 0)

End Class
